﻿

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using AjaxPro;
using Gift.Utile;


namespace WebApp.admin.goods
{
    public partial class goods : BaseAdmin
    {


        tbGoodsCategory oGoodsCategory = new tbGoodsCategory();
        tbGoods oGoods = new tbGoods();
 
       


        // Methods
        protected StringBuilder AddCategoryBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                ArrayList list = ArrayList.Adapter(BaseFactory.theGoods.GetModel(Convert.ToInt32(base.Request.QueryString["id"])).category.Split(new char[] { ',' }));
                foreach (string str in list)
                {
                    DataTable categoryByCategoryIdentity = BaseFactory.theCategory.GetCategoryByCategoryIdentity(Convert.ToInt32(str));
                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", categoryByCategoryIdentity.Rows[0]["categoryIdentity"], "\">", categoryByCategoryIdentity.Rows[0]["categoryName"], "</option>" }));
                }
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder AtrributesBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable goodsById = BaseFactory.theGoods.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
                DataTable goodsAttributeByGood = BaseFactory.theGoodsAttribute.GetGoodsAttributeByGood(Convert.ToInt32(base.Request.QueryString["id"]));
                if (goodsAttributeByGood.Rows.Count <= 0)
                {
                    return builder;
                }
                builder.AppendLine("<table cellspacing=\"0\" cellpadding=\"0\">");
                foreach (DataRow row in goodsAttributeByGood.Rows)
                {
                    DataTable attributeValueByAttribute;
                    ArrayList list;
                    DataTable table4;
                    ArrayList list2;
                    if (Convert.ToInt32(row["isRequired"]) == 0)
                    {
                        switch (Convert.ToInt32(row["attributeInputType"]))
                        {
                            case 0:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                                goto Label_0CE4;

                            case 1:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                                goto Label_0CE4;

                            case 2:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<65 || event.keyCode>90)&&(event.keyCode<97 || event.keyCode>122)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                                goto Label_0CE4;

                            case 3:
                                attributeValueByAttribute = BaseFactory.theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                list = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                                builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                                foreach (DataRow row2 in attributeValueByAttribute.Rows)
                                {
                                    if (list.Contains(row2["id"].ToString()))
                                    {
                                        builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" checked=\"checked\" />" + row2["value"].ToString() + "");
                                    }
                                    else
                                    {
                                        builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" />" + row2["value"].ToString() + "");
                                    }
                                }
                                builder.AppendLine("<input name=\"hdAtrributes\" type=\"hidden\" value=\"rdAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>");
                                goto Label_0CE4;

                            case 4:
                                table4 = BaseFactory.theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                list2 = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                                builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                                foreach (DataRow row3 in table4.Rows)
                                {
                                    if (list2.Contains(row3["id"].ToString()))
                                    {
                                        builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" checked=\"checked\"/>" + row3["value"].ToString() + "");
                                    }
                                    else
                                    {
                                        builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" />" + row3["value"].ToString() + "");
                                    }
                                }
                                builder.AppendLine("</td></tr>");
                                goto Label_0CE4;

                            case 5:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input id=\"txtAtrributes", row["id"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" onfocus=\"new WdatePicker(this,'%Y-%M-%D',true,'default')\" style=\"ime-mode:disabled\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                                goto Label_0CE4;
                        }
                    }
                    else
                    {
                        switch (Convert.ToInt32(row["attributeInputType"]))
                        {
                            case 0:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                                goto Label_0CE4;

                            case 1:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                                goto Label_0CE4;

                            case 2:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<65 || event.keyCode>90)&&(event.keyCode<97 || event.keyCode>122)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                                goto Label_0CE4;

                            case 3:
                                attributeValueByAttribute = BaseFactory.theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                list = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                                builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                                foreach (DataRow row2 in attributeValueByAttribute.Rows)
                                {
                                    if (list.Contains(row2["id"].ToString()))
                                    {
                                        builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" checked=\"checked\" />" + row2["value"].ToString() + "");
                                    }
                                    else
                                    {
                                        builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" />" + row2["value"].ToString() + "");
                                    }
                                }
                                builder.AppendLine("<input name=\"hdAtrributes\" type=\"hidden\" value=\"rdAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>");
                                goto Label_0CE4;

                            case 4:
                                table4 = BaseFactory.theAttributeValue.GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                                list2 = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                                builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                                foreach (DataRow row3 in table4.Rows)
                                {
                                    if (list2.Contains(row3["id"].ToString()))
                                    {
                                        builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" checked=\"checked\" />" + row3["value"].ToString() + "");
                                    }
                                    else
                                    {
                                        builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" />" + row3["value"].ToString() + "");
                                    }
                                }
                                builder.AppendLine("<input name=\"hdAtrributes\" type=\"hidden\" value=\"cbAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>");
                                goto Label_0CE4;

                            case 5:
                                builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" onfocus=\"new WdatePicker(this,'%Y-%M-%D',true,'default')\" style=\"ime-mode:disabled\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                                goto Label_0CE4;
                        }
                    }
                Label_0CE4: ;
                }
                builder.AppendLine("</table>");
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder BrandBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable goodsById = BaseFactory.theGoods.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
                Array array = goodsById.Rows[0]["category"].ToString().Split(new char[] { ',' });
                DataTable table2 = new DataTable();
                ArrayList list = new ArrayList();
                bool flag = false;
                foreach (string str in array)
                {
                    DataTable brandByCategoryIdentity = BaseFactory.theBrand.GetBrandByCategoryIdentity(Convert.ToInt32(str));
                    if (!flag)
                    {
                        table2 = brandByCategoryIdentity.Clone();
                        table2.Clear();
                        flag = true;
                    }
                    foreach (DataRow row in brandByCategoryIdentity.Rows)
                    {
                        if (!list.Contains(row["id"].ToString()))
                        {
                            list.Add(row["id"].ToString());
                            table2.Rows.Add(row.ItemArray);
                        }
                    }
                }
                foreach (DataRow row in table2.Rows)
                {
                    if (Convert.ToInt32(goodsById.Rows[0]["brand"]) == Convert.ToInt32(row["id"]))
                    {
                        builder.AppendLine("<option value=\"" + row["id"].ToString() + "\" selected=\"selected\" >" + row["brandName"].ToString() + "</option>");
                    }
                    else
                    {
                        builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["brandName"].ToString() + "</option>");
                    }
                }
            }
            catch
            {
            }
            return builder;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                this.oGoods = BaseFactory.theGoods.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                this.oGoods.category = base.Request.Form["addCategory"];
                this.oGoods.brand = Convert.ToInt32(base.Request.Form["brand"]);
                this.oGoods.goodsType = Convert.ToInt32(base.Request.Form["sGoodsType"]);
                this.oGoods.goodsName = this.txtGoodsName.Value;
               this.oGoods.goodsNo = this.txtGoodsNo.Value;
               this.oGoods.origin = this.txtOrigin.Value;
               this.oGoods.isBest = MathLibrary.ConvertNumToBool(base.Request.Form["isBest"]);
               this.oGoods.isNew = MathLibrary.ConvertNumToBool(base.Request.Form["isNew"]);
               this.oGoods.isTopSelling = MathLibrary.ConvertNumToBool(base.Request.Form["isTopSelling"]);
               this.oGoods.isShelves = MathLibrary.ConvertNumToBool(base.Request.Form["isShelves"]);
               this.oGoods.isProfShelves = MathLibrary.ConvertNumToBool(base.Request.Form["isProfShelves"]);
               this.oGoods.isPromote = MathLibrary.ConvertNumToBool(base.Request.Form["isPromote"]);
               this.oGoods.isUserDiscount = MathLibrary.ConvertNumToBool(base.Request.Form["isUserDiscount"]);
               this.oGoods.isOrderFromFreight = this.cbIsOrderFromFreight.Checked;
               this.oGoods.isDiscount = this.cbIsDiscount.Checked;
               this.oGoods.isTotalReduce = this.cbIsTotalReduce.Checked;
               this.oGoods.isGifts = this.cbIsGifts.Checked;
               this.oGoods.isSendIntegral = this.cbIsSendIntegral.Checked;
               this.oGoods.isFromFreight = MathLibrary.ConvertNumToBool(base.Request.Form["isFromFreight"]);
               this.oGoods.freightTemplate =  Convert.ToInt32(base.Request.Form["freightTemplate"]);
               this.oGoods.keywords = this.txtKeywords.Value;
               this.oGoods.description = this.txtaDescription.Value;
               this.oGoods.goodsRemark = this.txtGoodsRemark.InnerText.Replace("'", "''");

               this.oGoods.professionType = this.ddlProfessionType.SelectedItem.Value == "" ? 0 : int.Parse(this.ddlProfessionType.SelectedItem.Value);
               if (BaseFactory.theGoods.Update(this.oGoods))
                {
                    int num;
                    ArrayList list = ArrayList.Adapter(this.oGoods.category.Split(new char[] { ',' }));
                    ArrayList list2 = new ArrayList();
                    DataTable goodsCategoryByGoods = BaseFactory.theGoodsCategory.GetGoodsCategoryByGoods(this.oGoods.id);
                    this.oGoodsCategory.goods = this.oGoods.id;
                    foreach (DataRow row in goodsCategoryByGoods.Rows)
                    {
                        list2.Add(row["category"].ToString());
                        if (!list.Contains(row["category"].ToString()))
                        {
                            BaseFactory.theGoodsCategory.Delete(Convert.ToInt32(row["id"]));
                        }
                    }
                    foreach (string str in list)
                    {
                        if (!list2.Contains(str))
                        {
                            this.oGoodsCategory.category = Convert.ToInt32(str);
                            BaseFactory.theGoodsCategory.Add(this.oGoodsCategory);
                        }
                    }
                    ArrayList list3 = ArrayList.Adapter(base.Request.Form["hdGoodsEntitys"].Split(new char[] { ',' }));
                    ArrayList list4 = ArrayList.Adapter(base.Request.Form["barCode"].Split(new char[] { ',' }));
                    ArrayList list5 = ArrayList.Adapter(base.Request.Form["goodsArtno"].Split(new char[] { ',' }));
                    ArrayList list6 = ArrayList.Adapter(base.Request.Form["salePrice"].Split(new char[] { ',' }));
                    ArrayList list7 = ArrayList.Adapter(base.Request.Form["costPrice"].Split(new char[] { ',' }));
                    ArrayList list8 = ArrayList.Adapter(base.Request.Form["marketPrice"].Split(new char[] { ',' }));
                    ArrayList list9 = ArrayList.Adapter(base.Request.Form["giveIntegral"].Split(new char[] { ',' }));
                    ArrayList list10 = ArrayList.Adapter(base.Request.Form["goodsWeight"].Split(new char[] { ',' }));
                    ArrayList list11 = ArrayList.Adapter(base.Request.Form["selectWeightUnit"].Split(new char[] { ',' }));
                    ArrayList list12 = ArrayList.Adapter(base.Request.Form["inventory"].Split(new char[] { ',' }));
                    for (num = 0; num < list3.Count; num++)
                    {
                        tbGoodsEntitysSpecifications oGoodsEntitysSpecifications;
                        DataTable specifications;
                        tbGoodsEntitys oGoodsEntitys = new tbGoodsEntitys();
                        oGoodsEntitys.goods =this.oGoods.id;
                        oGoodsEntitys.barCode = list4[num].ToString();
                        oGoodsEntitys.goodsArtno = list5[num].ToString();
                        oGoodsEntitys.salePrice = Convert.ToDecimal(list6[num]);
                        oGoodsEntitys.costPrice = Convert.ToDecimal(list7[num]);
                        oGoodsEntitys.marketPrice = Convert.ToDecimal(list8[num]);
                        oGoodsEntitys.integral = 0;
                        oGoodsEntitys.giveIntegral = Convert.ToDecimal(list9[num]);
                        oGoodsEntitys.goodsWeight = Convert.ToDecimal(list10[num]);
                        oGoodsEntitys.weightUnit = list11[num].ToString();
                        oGoodsEntitys.inventory = Convert.ToInt32(list12[num]);
                        if (Convert.ToInt32(list3[num]) == 0)
                        {
                            oGoodsEntitys.id = BaseFactory.theGoodsEntitys.Add(oGoodsEntitys);
                            if (oGoodsEntitys.id != 0)
                            {
                                oGoodsEntitysSpecifications = new tbGoodsEntitysSpecifications();
                                specifications = BaseFactory.theSpecifications.GetSpecificationsByCompanyId(int.Parse(base.Administrator.CompanyId));
                                foreach (DataRow row in specifications.Rows)
                                {
                                    if (base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""] != null)
                                    {
                                        oGoodsEntitysSpecifications.goodsEntitys = oGoodsEntitys.id;
                                        oGoodsEntitysSpecifications.specifications = Convert.ToInt32(row["id"]);
                                        oGoodsEntitysSpecifications.specificationsValue = Convert.ToInt32(ArrayList.Adapter(base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""].Split(new char[] { (char)0x2c }))[num]);
                                        BaseFactory.theGoodsEntitysSpecifications.Add(oGoodsEntitysSpecifications);
                                    }
                                }
                            }
                        }
                        else
                        {
                            oGoodsEntitys.id = Convert.ToInt32(list3[num]);
                            if (BaseFactory.theGoodsEntitys.Update(oGoodsEntitys))
                            {
                                oGoodsEntitysSpecifications = new tbGoodsEntitysSpecifications();
                                specifications = BaseFactory.theSpecifications.GetSpecificationsByCompanyId(int.Parse(base.Administrator.CompanyId));
                                BaseFactory.theGoodsEntitysSpecifications.Delete(oGoodsEntitys.id);
                                foreach (DataRow row in specifications.Rows)
                                {
                                    if (base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""] != null)
                                    {
                                        oGoodsEntitysSpecifications.goodsEntitys = oGoodsEntitys.id;
                                        oGoodsEntitysSpecifications.specifications = Convert.ToInt32(row["id"]);
                                        oGoodsEntitysSpecifications.specificationsValue = Convert.ToInt32(ArrayList.Adapter(base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""].Split(new char[] { (char)0x2c }))[num]);
                                        BaseFactory.theGoodsEntitysSpecifications.Add(oGoodsEntitysSpecifications);
                                    }
                                }
                            }
                        }
                    }
                    HttpFileCollection files = HttpContext.Current.Request.Files;
                    if (base.Request.Form["imgAlt"] != null)
                    {
                        ArrayList list13 = ArrayList.Adapter(base.Request.Form["imgAlt"].Split(new char[] { ',' }));
                        ArrayList list14 = ArrayList.Adapter(base.Request.Form["hdGoodGallery"].Split(new char[] { ',' }));
                        for (num = 0; num < files.Count; num++)
                        {
                            tbGoodGallery oGoodGallery = new tbGoodGallery();
                            oGoodGallery.goods =this.oGoods.id;
                            if (Convert.ToInt32(list14[num]) != 0)
                            {
                                //DataTable table3;
                                tbGoodGallery oTempGoodGallery = BaseFactory.theGoodGallery.GetModel(Convert.ToInt32(list14[num]));
                                oGoodGallery.id = oTempGoodGallery.id;
                                if (files[num].FileName != "")
                                {
                                    File.Delete(base.Server.MapPath(oTempGoodGallery.thumbImg));
                                    File.Delete(base.Server.MapPath(oTempGoodGallery.smallImg));
                                    File.Delete(base.Server.MapPath(oTempGoodGallery.bigImg));
                                }
                                else
                                {
                                    oGoodGallery.bigImg = oTempGoodGallery.bigImg;
                                    oGoodGallery.smallImg = oTempGoodGallery.smallImg;
                                    oGoodGallery.thumbImg = oTempGoodGallery.thumbImg;
                                }
                            }
                            if (files[num].FileName != "")
                            {
                                string str2 = DateTime.Now.ToString("yyyyMMddHHssffffff");
                                oGoodGallery.bigImg = "/cn/goods/big/" + str2 + Path.GetExtension(files[num].FileName);
                                oGoodGallery.smallImg = "/cn/goods/small/" + str2 + Path.GetExtension(files[num].FileName);
                                oGoodGallery.thumbImg = "/cn/goods/thumb/" + str2 + Path.GetExtension(files[num].FileName);
                                files[num].SaveAs(base.Server.MapPath(oGoodGallery.bigImg));
                                ThumbnailImage image = new ThumbnailImage();
                                image.GetThumbnail(base.Server.MapPath(oGoodGallery.bigImg), base.Server.MapPath(oGoodGallery.smallImg), 500, 500, 1, System.Drawing.Color.White);
                                image.GetThumbnail(base.Server.MapPath(oGoodGallery.bigImg), base.Server.MapPath(oGoodGallery.thumbImg), 200, 200, 1, System.Drawing.Color.White);
                            }
                            oGoodGallery.imgAlt = list13[num].ToString();
                            if (Convert.ToInt32(list14[num]) != 0)
                            {
                                BaseFactory.theGoodGallery.Update(oGoodGallery);
                            }
                            else
                            {
                                BaseFactory.theGoodGallery.Add(oGoodGallery);
                            }
                        }
                    }
                    BaseFactory.theGoodsAttribute.DeleteGoodsAttributeByGoods(this.oGoods.id);
                    DataTable atrributesByGoodsTypeAndState = BaseFactory.theAttributes.GetAtrributesByGoodsTypeAndState(this.oGoods.goodsType, 1);
                    int num2 = 0;
                    if (atrributesByGoodsTypeAndState.Rows.Count > 0)
                    {
                        ArrayList list15 = new ArrayList();
                        if (base.Request.Form["atrributesValue"] != null)
                        {
                            list15 = ArrayList.Adapter(base.Request.Form["atrributesValue"].Split(new char[] { ',' }));
                        }
                        for (int i = 0; i < atrributesByGoodsTypeAndState.Rows.Count; i++)
                        {
                            tbGoodsAttribute goodsAttribute = new tbGoodsAttribute();
                            goodsAttribute.goods =this.oGoods.id;
                            goodsAttribute.attributes = Convert.ToInt32(atrributesByGoodsTypeAndState.Rows[i]["id"]);
                            switch (Convert.ToInt32(atrributesByGoodsTypeAndState.Rows[i]["attributeInputType"]))
                            {
                                case 0:
                                case 1:
                                case 2:
                                case 5:
                                    goodsAttribute.inputValue = (list15.Count > num2) ? list15[num2].ToString() : "";
                                    goodsAttribute.attributeValue = "0";
                                    BaseFactory.theGoodsAttribute.Add(goodsAttribute);
                                    num2++;
                                    break;

                                case 3:
                                    {
                                        string str3 = "rdAtrributes" + atrributesByGoodsTypeAndState.Rows[i]["id"].ToString();
                                        goodsAttribute.attributeValue = base.Request.Form[str3];
                                        goodsAttribute.inputValue = "";
                                        BaseFactory.theGoodsAttribute.Add(goodsAttribute);
                                        break;
                                    }
                                case 4:
                                    {
                                        string str4 = "cbAtrributes" + atrributesByGoodsTypeAndState.Rows[i]["id"].ToString();
                                        goodsAttribute.attributeValue = base.Request.Form[str4];
                                        goodsAttribute.inputValue = "";
                                        BaseFactory.theGoodsAttribute.Add(goodsAttribute);
                                        break;
                                    }
                            }
                        }
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='goodsList.aspx';</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            catch
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
        }



        private void BindProfessionAreaType()
        {
            DataSet dsType = new DataSet();
            DALProfessionAreaType theType = new DALProfessionAreaType();
            dsType = theType.GetList("");
            if (dsType.Tables.Count > 0)
            {
                this.ddlProfessionType.DataSource = dsType.Tables[0].DefaultView;
                this.ddlProfessionType.DataTextField = "Name";
                this.ddlProfessionType.DataValueField = "Id";
                this.ddlProfessionType.DataBind();
            }
            this.ddlProfessionType.Items.Insert(0, new ListItem("选择所属专区..", ""));


        }



        protected StringBuilder CategoryBind()
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                this.RecursionCategory(sb, "00000000-0000-0000-0000-000000000000", int.Parse(base.Administrator.CompanyId));
            }
            catch
            {
            }
            return sb;
        }

        protected StringBuilder FreightTemplateBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable freightTemplateByCompanyId = BaseFactory.theFreightTemplate.GetFreightTemplateByCompanyId(int.Parse(base.Administrator.CompanyId));
                DataTable goodsById = BaseFactory.theGoods.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
                builder.AppendLine("<select id=\"selectFreightTemplate\" name=\"freightTemplate\" onchange=\"setFreightTemplate();\">");
                builder.AppendLine("<option value=\"0\">请选择运费模版...</option>");
                foreach (DataRow row in freightTemplateByCompanyId.Rows)
                {
                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\">", row["freightTemplateName"], "</option>" }));
                }
                builder.AppendLine("</select>");
                //if (Convert.ToInt32(goodsById.Rows[0]["isFromFreight"]) == 0)
                //{
                    builder.AppendLine(string.Concat(new object[] { "<span id=\"spFreightTemplate\"><a onclick=\"openNewTab('设置运费方式','/admin/basic/freightWayList.aspx?tid=" + goodsById.Rows[0]["freightTemplate"] + "&gid=" + base.Request.QueryString["id"] + "')", "\">设置运费方式</a></span>" }));
                    return builder;
              //  }
             //   builder.AppendLine("<span id=\"spFreightTemplate\"></span>");
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder GoodGalleryBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable goodGalleryByGoods = BaseFactory.theGoodGallery.GetGoodGalleryByGoods(Convert.ToInt32(base.Request.QueryString["id"]));
                foreach (DataRow row in goodGalleryByGoods.Rows)
                {
                    builder.AppendLine("<tr>");
                    builder.AppendLine(string.Concat(new object[] { "<td><input name=\"hdGoodGallery\" type=\"hidden\" value=\"", row["id"], "\" /><img src=\"", row["thumbImg"].ToString(), "\" alt=\"", row["imgAlt"].ToString(), "\" style=\"width:50px; height:50px;\" /><br /><input name=\"fileGoodGallery\" type=\"file\" /><span class=\"imgClass\"> *分辨率大于500*500的等比例图片</span></td>" }));
                    builder.AppendLine("<td><input name=\"imgAlt\" type=\"text\" value=\"" + row["imgAlt"] + "\" /></td>");
                    builder.AppendLine("<td><input type=\"button\" class=\"formButton\" value=\"删除\" title=\"删除图片\" onclick=\"delGalleryInput(this.parentNode.parentNode.rowIndex," + row["id"].ToString() + ");\"/></td>");
                    builder.AppendLine("</tr>");
                }
            }
            catch
            {
            }
            return builder;
        }

        private void GoodsBind()
        {
            try
            {
                DataTable goodsById = BaseFactory.theGoods.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
                if (goodsById.Rows.Count > 0)
                {
                    DataRow row = goodsById.Rows[0];
                    this.txtGoodsName.Value = row["goodsName"].ToString();
                    this.txtGoodsNo.Value = row["goodsNo"].ToString();
                    this.txtOrigin.Value = row["origin"].ToString();
                    if (Convert.ToInt32(row["isBest"]) == 0)
                    {
                        this.rdIsBest0.Checked = true;
                    }
                    else
                    {
                        this.rdIsBest1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isNew"]) == 0)
                    {
                        this.rdIsNew0.Checked = true;
                    }
                    else
                    {
                        this.rdIsNew1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isTopSelling"]) == 0)
                    {
                        this.rdIsTopSelling0.Checked = true;
                    }
                    else
                    {
                        this.rdIsTopSelling1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isShelves"]) == 0)
                    {
                        this.rdIsShelves0.Checked = true;
                    }
                    else
                    {
                        this.rdIsShelves1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isProfShelves"]) == 0)
                    {
                        this.rdIsProfShelves0.Checked = true;
                    }
                    else
                    {
                        this.rdIsProfShelves1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isPromote"]) == 0)
                    {
                        this.rdIsPromote0.Checked = true;
                    }
                    else
                    {
                        this.rdIsPromote1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isUserDiscount"]) == 0)
                    {
                        this.rdIsUserDiscount0.Checked = true;
                    }
                    else
                    {
                        this.rdIsUserDiscount1.Checked = true;
                    }
                    if (Convert.ToInt32(row["isOrderFromFreight"]) == 0)
                    {
                        this.cbIsOrderFromFreight.Checked = false;
                    }
                    else
                    {
                        this.cbIsOrderFromFreight.Checked = true;
                    }
                    if (Convert.ToInt32(row["isDiscount"]) == 0)
                    {
                        this.cbIsDiscount.Checked = false;
                    }
                    else
                    {
                        this.cbIsDiscount.Checked = true;
                    }
                    if (Convert.ToInt32(row["isTotalReduce"]) == 0)
                    {
                        this.cbIsTotalReduce.Checked = false;
                    }
                    else
                    {
                        this.cbIsTotalReduce.Checked = true;
                    }
                    if (Convert.ToInt32(row["isGifts"]) == 0)
                    {
                        this.cbIsGifts.Checked = false;
                    }
                    else
                    {
                        this.cbIsGifts.Checked = true;
                    }
                    if (Convert.ToInt32(row["isSendIntegral"]) == 0)
                    {
                        this.cbIsSendIntegral.Checked = false;
                    }
                    else
                    {
                        this.cbIsSendIntegral.Checked = true;
                    }
                    if (Convert.ToInt32(row["isFromFreight"]) == 0)
                    {
                    
                        this.rdIsFromFreight0.Checked = true;
                    }
                    else
                    {
                  //      base.ClientScript.RegisterStartupScript(base.GetType(), "sFT", "<script language=\"javascript\" defer>$(\"#trFreightTemplate\").hide();</script>");
                        this.rdIsFromFreight1.Checked = true;
                    }
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sFT", "<script language=\"javascript\" defer>$(\"#trFreightTemplate\").show();selectIsExitItem(\"selectFreightTemplate\",\"" + row["freightTemplate"].ToString() + "\");</script>");

                    if (Convert.ToInt32(row["professionType"]) != 0)
                    {
                        this.ddlProfessionType.Items.FindByValue(row["professionType"].ToString()).Selected = true;
                    }

                    this.txtKeywords.Value = row["keywords"].ToString();
                    this.txtaDescription.Value = row["description"].ToString();
                    this.txtGoodsRemark.InnerText  = row["goodsRemark"].ToString().Replace("''", "'");
                    this.txtGoodsName.Focus();
                }
            }
            catch
            {
            }
        }

        protected StringBuilder GoodsEntitysBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataRow current;
                DataTable goodsEntitysByGoods = BaseFactory.theGoodsEntitys.GetGoodsEntitysByGoods(Convert.ToInt32(base.Request.QueryString["id"]));
                DataTable specificationsNameByGoodsEntitys = new DataTable();
                if (goodsEntitysByGoods.Rows.Count > 0)
                {
                    specificationsNameByGoodsEntitys = BaseFactory.theSpecifications.GetSpecificationsNameByGoodsEntitys(Convert.ToInt32(goodsEntitysByGoods.Rows[0]["id"]));
                }
                builder.AppendLine("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"GoodsEntitysSpecifications\">");
                builder.AppendLine("<tr><td>条形码</td><td>货号</td>");
                IEnumerator enumerator = specificationsNameByGoodsEntitys.Rows.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    current = (DataRow)enumerator.Current;
                    builder.AppendLine("<td><span id=\"spanS" + current["id"].ToString() + "\">" + current["specificationsName"].ToString() + "</span></td>");
                }

                builder.AppendLine("<td>销售价</td><td>成本价</td><td>市场价</td><td>赠送积分</td><td>重量</td><td>重量单位</td><td>库存</td><td>操作</td></tr> ");
                for (int i = 0; i < goodsEntitysByGoods.Rows.Count; i++)
                {
                    current = goodsEntitysByGoods.Rows[i];
                    builder.AppendLine(string.Concat(new object[] { "<tr><td><input name=\"hdGoodsEntitys\" type=\"hidden\" value=\"", current["id"], "\" /><input name=\"barCode\" type=\"text\" class=\"specifications\" value=\"", current["barCode"].ToString(), "\" /></td>" }));
                    builder.AppendLine("<td><input name=\"goodsArtno\" type=\"text\" class=\"specifications\" value=\"" + current["goodsArtno"].ToString() + "\" /></td>");
                    foreach (DataRow row2 in specificationsNameByGoodsEntitys.Rows)
                    {
                        DataTable specificationsValueBySpecifications = BaseFactory.theSpecificationsValue.GetSpecificationsValueBySpecifications(Convert.ToInt32(row2["id"]));
                        builder.AppendLine("<td><select name=\"SpecificationsValue" + row2["id"].ToString() + "\">");
                        DataTable goodsEntitysSpecificationsByGoodsEntitysAndSpecifications = BaseFactory.theGoodsEntitysSpecifications.GetGoodsEntitysSpecificationsByGoodsEntitysAndSpecifications(Convert.ToInt32(current["id"]), Convert.ToInt32(row2["id"]));
                        foreach (DataRow row3 in specificationsValueBySpecifications.Rows)
                        {
                            if (goodsEntitysSpecificationsByGoodsEntitysAndSpecifications.Rows.Count > 0)
                            {
                                if (Convert.ToInt32(row3["id"]) == Convert.ToInt32(goodsEntitysSpecificationsByGoodsEntitysAndSpecifications.Rows[0]["specificationsValue"]))
                                {
                                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\" selected=\"selected\">", row3["specificationsValueName"], "</option>" }));
                                }
                                else
                                {
                                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\">", row3["specificationsValueName"], "</option>" }));
                                }
                            }
                            else
                            {
                                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\">", row3["specificationsValueName"], "</option>" }));
                            }
                        }
                        builder.AppendLine("</select></td>");
                    }
                    builder.AppendLine("<td><input name=\"salePrice\" type=\"text\" class=\"specifications\" value=\"" + current["salePrice"].ToString() + "\" /></td>");
                    builder.AppendLine("<td><input name=\"costPrice\" type=\"text\" class=\"specifications\" value=\"" + current["costPrice"].ToString() + "\" /></td>");
                    builder.AppendLine("<td><input name=\"marketPrice\" type=\"text\" class=\"specifications\" value=\"" + current["marketPrice"].ToString() + "\" /></td>");
                    builder.AppendLine("<td><input name=\"giveIntegral\" type=\"text\" class=\"specifications\" value=\"" + current["giveIntegral"].ToString() + "\" /></td>");
                    builder.AppendLine("<td><input name=\"goodsWeight\" type=\"text\" class=\"specifications\" value=\"" + current["goodsWeight"].ToString() + "\" /></td>");
                    if (current["weightUnit"].ToString() == "克")
                    {
                        builder.AppendLine("<td><select name=\"selectWeightUnit\"><option value=\"克\" selected=\"selected\">克 </option><option value=\"千克\">千克 </option><option value=\"吨\">吨 </option></select></td>");
                    }
                    else if (current["weightUnit"].ToString() == "千克")
                    {
                        builder.AppendLine("<td><select name=\"selectWeightUnit\"><option value=\"克\">克 </option><option value=\"千克\" selected=\"selected\">千克 </option><option value=\"吨\">吨 </option></select></td>");
                    }
                    else
                    {
                        builder.AppendLine("<td><select name=\"selectWeightUnit\"><option value=\"克\">克 </option><option value=\"千克\">千克 </option><option value=\"吨\" selected=\"selected\">吨 </option></select></td>");
                    }
                    builder.AppendLine("<td><input name=\"inventory\" type=\"text\" class=\"specifications\" value=\"" + current["inventory"].ToString() + "\" /></td>");
                    if (i == 0)
                    {
                        builder.AppendLine("<td><input id=\"btnAddGoodsEntitys\" type=\"button\" class=\"formButton\" value=\"添加\" title=\"添加货品\" onclick=\"AddGoodsEntitys();\" /></td>");
                    }
                    else
                    {
                        builder.AppendLine("<td><input type=\"button\" class=\"formButton\" value=\"删除\" title=\"删除货品\" onclick=\"delGoodsEntitys(this.parentNode.parentNode.rowIndex," + current["id"].ToString() + ");\"></td>");
                    }
                }
                builder.AppendLine("</table>");
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder GoodsTypeBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable goodsById = BaseFactory.theGoods.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
                DataTable goodsType = BaseFactory.theGoodsType.GetGoodsTypeByCompanyId(int.Parse(base.Administrator.CompanyId));
                foreach (DataRow row in goodsType.Rows)
                {
                    if (Convert.ToInt32(goodsById.Rows[0]["goodsType"]) == Convert.ToInt32(row["id"]))
                    {
                        builder.AppendLine("<option value=\"" + row["id"].ToString() + "\" selected=\"selected\">" + row["typeName"].ToString() + "</option>");
                    }
                    else
                    {
                        builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["typeName"].ToString() + "</option>");
                    }
                }
            }
            catch
            {
            }
            return builder;
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(inArray));
        }

        protected void Page_Load(object sender, EventArgs e)
        {

           
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.GoodsEdit);
                if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.GoodsEdit) || Request.QueryString["show"] == "1")
                {
                    this.btnSubmit.Visible = false;
                }
                this.BindProfessionAreaType();
            }
           
            Utility.RegisterTypeForAjax(typeof(Goods_ajax));
            if (!(base.IsPostBack || (base.Request.QueryString["id"] == null)))
            {
                this.GoodsBind();
            }

        }

        private void RecursionCategory(StringBuilder sb, string parentCategory, int companyId)
        {
            DataTable table = BaseFactory.theCategory.GetCategoryByParentIdAndCompanyIdAndIsShow(parentCategory, companyId, -1);
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    string str = "";
                    int num = Convert.ToInt32(row["depth"]);
                    if (num > 0)
                    {
                        for (int i = 0; i < num; i++)
                        {
                            str = str + "&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        str = str + "┣";
                    }
                    sb.AppendLine("<option value=\"" + row["categoryIdentity"].ToString() + "\">" + str + row["categoryName"].ToString() + "</option>");
                    this.RecursionCategory(sb, row["id"].ToString(), companyId);
                }
            }
        }

        protected override void SavePageStateToPersistenceMedium(object pViewState)
        {
            LosFormatter formatter = new LosFormatter();
            StringWriter output = new StringWriter();
            formatter.Serialize(output, pViewState);
            string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
            base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
        }

        protected StringBuilder SpecificationsBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable specificationsByCompanyId = BaseFactory.theSpecifications.GetSpecificationsByCompanyId(int.Parse(base.Administrator.CompanyId));
                DataTable goodsEntitysSpecificationsByGoods = BaseFactory.theGoodsEntitysSpecifications.GetGoodsEntitysSpecificationsByGoods(Convert.ToInt32(base.Request.QueryString["id"]));
                ArrayList list = new ArrayList();
                foreach (DataRow row in goodsEntitysSpecificationsByGoods.Rows)
                {
                    list.Add(Convert.ToInt32(row["specifications"]));
                }
                bool flag = false;
                foreach (DataRow row in specificationsByCompanyId.Rows)
                {
                    builder.AppendLine("" + (!flag ? "" : "<br />") + "<input id=\"cb" + row["id"].ToString() + "\" value=\"" + row["id"].ToString() + "\" name=\"ss\" type=\"checkbox\" " + (list.Contains(Convert.ToInt32(row["id"])) ? "checked='checked'" : "") + " onclick=\"getSpecifications(" + row["id"].ToString() + ",this);\" />[" + row["specificationsName"].ToString() + "]");
                    flag = true;
                }
            }
            catch
            {
            }
            return builder;
        }

    }
}